package com.jinlejia.erp.ui.house.prospect;


import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.FragmentProspectUploadVillaBinding;
import com.jinlejia.erp.ui.house.prospect.bean.SurveyPictureBean;

/**
 * 上传或修改别墅类房源的图片
 */
public class ProspectUploadVillaFragment extends Fragment implements ProspectUploadImageActivity.HousePictureListener {

    public ProspectUploadVillaFragment() {
        // Required empty public constructor
    }


    private FragmentHelper fragmentHelper;
    private Helper helper;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        helper = Helper.init(this.getContext());
    }

    private FragmentProspectUploadVillaBinding binding;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_prospect_upload_villa, container, false);
        binding = DataBindingUtil.bind(view);
        if (getArguments() != null) {
            Bundle bundle = getArguments();
            String houseType = bundle.getString(Type.PARAM.HOUSE_TYPE);
            String houseSourceCode = bundle.getString(Type.PARAM.HOUSE_SOURCE_CODE);
            String houseSourceType = bundle.getString(Type.PARAM.HOUSE_SOURCE_TYPE);
            fragmentHelper = FragmentHelper.init(this,houseSourceCode,houseSourceType,helper);
            Helper.Util.HouseInfo info = helper.util().formatHouseInfo(houseType);
            initView(info);
        }
        SurveyPictureBean bean = fragmentHelper.getSourceInfo();
        initView(bean);
        return view;
    }

    private void initView(Helper.Util.HouseInfo info) {
        int roomCount = info.getRoomCount();
        int drawingRoomCount = info.getDrawingCount();
        int toiletCount = info.getToiletCount();
        int kitchenCount = info.getKitchenCount();
        if (roomCount != 0) { //室 must
            fragmentHelper.createAdapterAndSetToGridView(binding.roomGridView,binding.roomHeaderView,
                    roomCount,true,true);
        } else {
            binding.roomHeaderView.setVisibility(View.GONE);
            binding.roomGridView.setVisibility(View.GONE);
            binding.roomLineView.setVisibility(View.GONE);
        }
        if (drawingRoomCount != 0) { //厅 must
            fragmentHelper.createAdapterAndSetToGridView(binding.drawingRoomGridView,binding.drawingRoomHeaderView,
                    drawingRoomCount,true,true);
        } else {
            binding.drawingRoomLineView.setVisibility(View.GONE);
            binding.drawingRoomHeaderView.setVisibility(View.GONE);
            binding.drawingRoomGridView.setVisibility(View.GONE);
        }
        if (toiletCount != 0) { //卫 must
            fragmentHelper.createAdapterAndSetToGridView(binding.toiletGridView,binding.toiletHeaderView,
                    toiletCount,true,true);
        } else {
            binding.toiletHeaderView.setVisibility(View.GONE);
            binding.toiletGridView.setVisibility(View.GONE);
            binding.toiletLineView.setVisibility(View.GONE);
        }
        if (kitchenCount != 0) { //厨 must
            fragmentHelper.createAdapterAndSetToGridView(binding.kitchenGridView,binding.kitchenHeaderView,
                    kitchenCount,true,true);
        } else {
            binding.kitchenHeaderView.setVisibility(View.GONE);
            binding.kitchenGridView.setVisibility(View.GONE);
            binding.kitchenLineView.setVisibility(View.GONE);
        }

        //院落 must
        fragmentHelper.createAdapterAndSetToGridView(binding.courtyardGridView,binding.courtyardHeaderView,
                4,true,false);

        //楼体正面全景 must 1
        fragmentHelper.createAdapterAndSetToGridView(binding.panoramaGridView,binding.panoramaHeaderView,
                1,true,false);

        /*以下的为非必填项,注释后的数字代表 最多传几张照片*/
        //车位 1
        fragmentHelper.createAdapterAndSetToGridView(binding.truckSpaceGridView,binding.truckSpaceHeaderView,
                1,false,false);

        //阳台 4
        fragmentHelper.createAdapterAndSetToGridView(binding.balconyGridView,binding.balconyHeaderView,
                4,false,true);

        //露台 2
        fragmentHelper.createAdapterAndSetToGridView(binding.gazeboGridView,binding.gazeboHeaderView,
                2,false,true);

        //玄关 2
        fragmentHelper.createAdapterAndSetToGridView(binding.hallwayGridView,binding.hallwayHeaderView,
                2,false,false);

        //储物间 3
        fragmentHelper.createAdapterAndSetToGridView(binding.storageRoomGridView,binding.storageRoomHeaderView,
                3,false,true);

        //衣帽间 3
        fragmentHelper.createAdapterAndSetToGridView(binding.cloakroomGridView,binding.cloakroomHeaderView,
                3,false,true);

        //洗衣间 2
        fragmentHelper.createAdapterAndSetToGridView(binding.laundryRoomGridView,binding.laundryRoomHeaderView,
                2,false,true);

        //保姆间 2
        fragmentHelper.createAdapterAndSetToGridView(binding.babySitterRoomGridView,binding.babySitterRoomHeaderView,
                2,false,true);

        //阳光房 1
        fragmentHelper.createAdapterAndSetToGridView(binding.sunlightRoomGridView,binding.sunlightRoomHeaderView,
                1,false,true);

        //游戏室 1
        fragmentHelper.createAdapterAndSetToGridView(binding.gameRoomGridView,binding.gameRoomHeaderView,
                1,false,true);

        //棋牌室 1
        fragmentHelper.createAdapterAndSetToGridView(binding.chessRoomGridView,binding.chessRoomHeaderView,
                1,false,true);

        //地下室 5
        fragmentHelper.createAdapterAndSetToGridView(binding.basementGridView, binding.basementHeaderView,
                5,false,true);

        //阁楼 5
        fragmentHelper.createAdapterAndSetToGridView(binding.atticGridView, binding.atticHeaderView,
                5,false,true);
    }

    private void initView(SurveyPictureBean bean) {
        if (bean == null) return;
        //室 must
        fragmentHelper.updateAdapterSource(binding.roomGridView, binding.roomHeaderView);

        //厅 must
        fragmentHelper.updateAdapterSource(binding.drawingRoomGridView, binding.drawingRoomHeaderView);

        //卫 must
        fragmentHelper.updateAdapterSource(binding.toiletGridView, binding.toiletHeaderView);

        //厨 must
        fragmentHelper.updateAdapterSource(binding.kitchenGridView, binding.kitchenHeaderView);


        //院落 must
        fragmentHelper.updateAdapterSource(binding.courtyardGridView, binding.courtyardHeaderView);

        //楼体正面全景 must 1
        fragmentHelper.updateAdapterSource(binding.panoramaGridView, binding.panoramaHeaderView);

        /*以下的为非必填项,注释后的数字代表 最多传几张照片*/
        fragmentHelper.updateAdapterSource(binding.truckSpaceGridView,binding.truckSpaceHeaderView);

        //阳台 4
        fragmentHelper.updateAdapterSource(binding.balconyGridView, binding.balconyHeaderView);

        //露台 2
        fragmentHelper.updateAdapterSource(binding.gazeboGridView, binding.gazeboHeaderView);

        //玄关 2
        fragmentHelper.updateAdapterSource(binding.hallwayGridView, binding.hallwayHeaderView);

        //储物间 3
        fragmentHelper.updateAdapterSource(binding.storageRoomGridView, binding.storageRoomHeaderView);

        //衣帽间 3
        fragmentHelper.updateAdapterSource(binding.cloakroomGridView, binding.cloakroomHeaderView);

        //洗衣间 2
        fragmentHelper.updateAdapterSource(binding.laundryRoomGridView, binding.laundryRoomHeaderView);

        //保姆间 2
        fragmentHelper.updateAdapterSource(binding.babySitterRoomGridView, binding.babySitterRoomHeaderView);

        //阳光房 1
        fragmentHelper.updateAdapterSource(binding.sunlightRoomGridView, binding.sunlightRoomHeaderView);

        //游戏室 1
        fragmentHelper.updateAdapterSource(binding.gameRoomGridView, binding.gameRoomHeaderView);

        //棋牌室 1
        fragmentHelper.updateAdapterSource(binding.chessRoomGridView, binding.chessRoomHeaderView);

        //地下室
        fragmentHelper.updateAdapterSource(binding.basementGridView, binding.basementHeaderView);

        //阁楼
        fragmentHelper.updateAdapterSource(binding.atticGridView, binding.atticHeaderView);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        fragmentHelper.onActivityResult(requestCode,resultCode,data);
    }

    @Override
    public String checkPicturesResult() {
        return fragmentHelper.checkPicturesResult();
    }

    @Override
    public SurveyPictureBean getPicturesResult() {
        return fragmentHelper.getPicturesResult();
    }
}
